package com.sheng.leetcode.year2022.month02.day16;

import com.sheng.leetcode.year2022.month02.day16.entity.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/16 11:01
 * @author liusheng
 */
public class LeetCode0002 {

    @Test
    public void test01(){
        // l1 = [2,4,3], l2 = [5,6,4];
        ListNode l1 = new ListNode(2);
        l1.next = new ListNode(4);
        l1.next.next = new ListNode(3);

        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);
        Solution solution = new Solution();
        solution.addTwoNumbers(l1, l2);
    }

}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public static int test1(ListNode listNode){
        ListNode temp = listNode;
        String str = "";

        while(true) {
            if (temp.next != null) {
                str = str + temp.val;
            }else {
                str = str + temp.val;
                break;
            }
            temp = temp.next;
        }

        char[] c1 = str.toCharArray();
        char[] c2 = new char[c1.length];
        for (int i = 0; i < c1.length; i++) {
            c2[i] = c1[c1.length - i - 1];
        }
        str = new String(c2);
        int i = Integer.parseInt(str);
        return i;
    }
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int i1 = test1(l1);
        int i2 = test1(l2);
        int i = i1 + i2;
        String str = i + "";
        int length = str.length();
        int[] ints = new int[length];
        for (int j = 0; j < length; j++) {
            ints[j] = i%10;
            i /= 10;
        }

        ListNode list = null;
        for (int j = 0; j < length; j++) {
            ListNode listNode = new ListNode(ints[j]);
            System.out.println(listNode.val);
            if (list == null) {
                list = listNode;
            }else {
                ListNode temp = list;
                temp = list;
                while (true) {
                    if (temp.next != null){
                        temp = list.next;
                    }else {
                        break;
                    }
                }
                temp.next = listNode;

            }
        }
        return list;
    }
}
